package ModelLayer;
import java.util.ArrayList;

/**
 * This class handle the details of the DvdContainer
 * 
 * @version (3 nov 2011)
 */
public class DvdContainer
{
    public ArrayList<Dvd> dv;
    //public ArrayList<eksemplar> ex;
    private static DvdContainer instance;
    private LoanContainer lc;
    private Dvd myDvd;
    

    /**
     * Constructor for objects of class DvdContainer
     * @param dv The ArrayList of dvd
     * @param ex The ArrayList of the different dvd copys
     */
    private DvdContainer()
    {
        dv = new ArrayList<Dvd>();
        //ex = new ArrayList<eksemplar>();
    }
    
    /**
     * Singleton.
     * @param   DvdContainer The new container.
     */
    public static DvdContainer getInstance()
    {
        if(instance == null)
        {
            instance = new DvdContainer();
        }
        return instance;
    }
    
    /**
     * Add a DVD to the collection.
     * @param dvd The dvd to be added.
     */
    public void addDvd(Dvd dvds)
    {
        dv.add(dvds);
    }
    
    /*public void addEksemplar(eksemplar eks)
    {
        ex.add(eks);
    }
    
    public void removeEksempla(eksemplar eks)
    {
        ex.remove(eks);
    }*/
    /**
     * Search a DVD in the DVD collection.
     * @param id The id of the dvd.
     * @param title The title off the dvd.
     * @param kunstner The name of the .
     * @param udgivelsesdato The date of the dvd creation.
     * @return dvd A object of the found dvd.
     */
    public Dvd searchDvd(long id, String title, String kunstner, String udgivelsesdato)
    {
        int index = 0;
        int index2 = 0;
        boolean found = false;
        while(index < dv.size())
        {
            Dvd dvds = dv.get(index);
            if(dvds.getId() == id && title == null && kunstner == null && udgivelsesdato == null)
            {
                found = true;
                index2 = index;
            }
            else if(dvds.getTitel().equals(title) && id == 0 && kunstner == null && udgivelsesdato == null)
            { 
                found = true;
                index2 = index;
            }
            else if(dvds.getKunstner().equals(kunstner) && title == null && id == 0 && udgivelsesdato == null)
            {
                found = true;
                index2 = index;
            }
            else if(dvds.getUdgivelsesDato().equals(udgivelsesdato) && title == null && id == 0 && udgivelsesdato == null)
            {
                found = true;
                index2 = index;
            }
            index++;
        }
        if(found == true)
        {
            return dv.get(index2);
        }
        else
        {
            return null;
        }
    }
    
    /**
     * Give a List a single dvd in the arcive
     * @param id The ID of the dvd to list
     */
    public void listDvd(long id)
    {
        for(Dvd dvdes : dv)
        {
            if(dvdes.getId() == id)
            {
                dvdes.print();
            }
        }
    }
    
    public void listEksemplarFromId(long id)
    {
        Dvd dvd = searchDvd(id, null, null, null);
        if(dvd != null)
        {
            dvd.listAllEksemplar();
        }
        else
        {
            System.out.println("This Dvd has no copies");
        }
    }
    
   /**
    * Search DvdContainer for a specifick serial number
    * @param serienr The serial number of the dvd copy
    * @return eksemplar Return the detail of the eksemplar
    */
/*   public eksemplar searchEksemplar(String serienr)
   {
       int index = 0;
       int index2 = 0;
       boolean found = false;
       while(index < ex.size())
       {
           eksemplar eks = ex.get(index);
           if(eks.getSerienr().equals(serienr))
           {
               found = true;
               index2 = index;
           }
           index++;
       }
       return ex.get(index2);
   }
*/   
   /**
    * Delete a dvd in the DvdContainer
    * @param id The id of the dvd to deleate
    */
   public void deleteDvd(long id)
   {
       int index = 0;
       boolean found = false;
       while(index < dv.size() && !found)
       {
           Dvd dvds = dv.get(index);
           if(dvds.getId() == id)
           {
               found = true;
               dv.remove(dvds);
               
           }
           index++;
       }
   }
   
    /**
    * Prints a complete list of all dvds in the DvdContainer
    */
   public void listDvd()
    {
        for(Dvd dvds : dv)
        {
            dvds.print();
            
        }
    }  
     /**
     * Create a copy of a dvd into a new eksemplar
     * @param id The id of the dvd to copy
     * @param serienr The serial number of the dvd
     * @anskafdato The date the dvd is bought
     * @param anskafpris The price for the dvd
     * @param dvd The description of the dvd
     * @return text Return a text message
     */
   
    public String createEksemplar(long id, String serienr, String anskafdato, String anskafpris, String dvd)
    {
        Dvd dvds = searchDvd(id, null, null, null);
        String text = null;
        if(dvds != null)
        {
            dvds.createEksemplar(serienr, anskafdato, anskafpris, dvd);
            

        }
        else
        {
            text = "Der findes ingen dvd med det Id";
        }
        return text;
    }
    
    public void searchEksemplar(String serienr)
    {
        int index = 0;
        while(index < dv.size())
        {
            Dvd dvd = dv.get(index);
            dvd.searchEksemplar(serienr);
            index++;
        }
        
    }
    
    public void deleteEksemplar(String serienr)
    {
        int index = 0;
        while(index < dv.size())
        {
            Dvd dvd = dv.get(index);
            dvd.deleteEksemplar(serienr);
            index++;
        }
    }
    
    public void listAll(long id)
    {
        int index = 0;
        while(index < dv.size())
        {
            Dvd dvds = dv.get(index);
            if(dvds.getId() == id)
            {
                dvds.print();
            }
            index++;
        }
    }
    
/*   
    public void listEksemplar()
    {
        for(eksemplar eks : ex)
        {
            eks.print();
            System.out.println("  ");
        }
    }
    
    public void listEksemplar(long id)
    {
        int index = 0;
        int index2 = 0;
        
        while( (index < dv.size()))
        {
            Dvd dvd = dv.get(index);
            {
                if(dvd.getId() == id)
                {
                    dvd.print();
                }
                index++;
            }
        }
    }
    
    public void deleteEksemplar(String serienr)
    {
        
        for(Dvd dvd : dv)
        {
            if(dvd.searchEksemplar2(serienr) == serienr)
            {
                dvd.deleteEksemplar(serienr);
                
            }
        }
        
        
        
    }
*/    
}
